unit CardKingdomClasses;

interface

uses
  Windows, IniFiles, ActiveX, msxml, ComObj, Classes, StrUtils,
  SysUtils, Dialogs, IdHTTP, IdComponent, Contnrs, DStrUtils;

Type

  TCardKingdomRow = class(TObject)
    ImageFileName, ImageURL, Name, URL: String;
    ImageRequest: TIdHTTP;
    ImageStream: TMemoryStream;
    constructor Create;
    procedure DownloadImage(URL: String; Path: String);
  end;

  TCardKingdomListPage = class(TObject)
    RowsCount: Integer;
    Rows: Array[0..50] of TCardKingdomRow;
    constructor Create;
    procedure ParseRows(Source: WideString);
    /////////////////////////////////////////
    procedure DumpToDB(CardName: String; CardURL: String; ImageURL: String; ImageFileName: String; SectionName: String);
  Private
    DataBase: TMemIniFile;
  end;

//  TCardPage = class(TObject)
//    constructor Init(DB: String; DefaultImageSavePath: String);
//    destructor Destroy; override;
//
//    procedure ParseIt(HTMLSource: String);
//
//    procedure GetAll(URL: String);
//  Private
//
//    URL,Name: String;
//
//    ImageURL, gURL: String;
//    ImageFileName,ImagePath: String;
//
//    CardName: String;
//    Request: IXMLHTTPRequest;
//    //MyParser: TclHTMLParser;
//    Completed: Boolean;
//    EmptyParam: OLEVariant;
//  end;

implementation

constructor TCardKingdomRow.Create;
begin
  inherited;
end;

constructor TCardKingdomListPage.create;
begin
  RowsCount := 50;
end;

procedure TCardKingdomListPage.ParseRows(Source: WideString);
var
  I: Integer;

  SearchOffset: Integer;

  // Immediate prefixes and suffixes of the strings to be extracted.
  ImageURLPrefix, ImageURLSuffix,
  URLPrefix, URLSuffix,
  NamePrefix, NameSuffix,
  SourceInternal: String;

  // Indexes of these prefixes and suffixes.
  ImageURLS, ImageURLE,
  URLS, URLE,
  NameS, NameE: Integer;
begin

  ImageURLPrefix := '<!--<td><a href="';
  ImageURLSuffix := '"';

  URLPrefix := '<a href="';
  URLSuffix := '"';

  NamePrefix := '">';
  NameSuffix := '<';

  SearchOffset := PosEx('<tr bgcolor=#eeeeee>',Source,1);

  for I := 0 to RowsCount - 1 do
  begin
    // Create a Row instance - to be manipulated.
    Rows[I] := TCardKingdomRow.Create;

    // Offset the scan point to just-after-the-beginning of the row.
    // CAUTION! DUMMY DATA!
    SearchOffset := PosEx('<!--<td>',Source,SearchOffset);

    // Get the ImageURL, put in a row array item.
    ImageURLS := PosEx(ImageURLPrefix,Source,SearchOffset);
    ImageURLE := PosEx(ImageURLSuffix,Source,Length(ImageURLPrefix)+ImageURLS);
    Rows[I].ImageURL := Copy(Source,Length(ImageURLPrefix)+ImageURLS,ImageURLE-(Length(ImageURLPrefix)+ImageURLS));

    // Derive the local filename from the online filename of the image.
    with Rows[I] do
    begin
      ImageFileName := Copy(ImageURL, LastDelimiter('/',ImageURL)+1,Length(ImageURL)-LastDelimiter('/',ImageURL)+1);
    end;

    // Move the search start point forward.
    SearchOffset := PosEx('/></a></td>-->',Source,ImageURLE);
    SearchOffset := PosEx('<td>',Source,SearchOffset);

    // Get the URL, put in a row array item.
    URLS := PosEx(URLPrefix,Source,SearchOffset);
    URLE := PosEx(URLSuffix,Source,Length(URLPrefix)+URLS);
    Rows[I].URL := Copy(Source,Length(URLPrefix)+URLS,URLE-(Length(URLPrefix)+URLS));

    // Move the search start point forward a little more.
    SearchOffset := PosEx('">',Source,SearchOffset+Length('<td>'));

    // Get the Name, put in a row array item.
    NameS := PosEx(NamePrefix,Source,SearchOffset);
    NameE := PosEx(NameSuffix,Source,Length(NamePrefix)+NameS);
    Rows[I].Name := Copy(Source,NameS+Length(NamePrefix),NameE-(Length(NamePrefix)+NameS));

    // Store the last point that the search was up to. This will be the start point
    // when the loop wraps round to the next search.
    SearchOffset := NameE;
  end;
end;

procedure TCardKingdomListPage.DumpToDB(
CardName: String;
CardURL: String;
ImageURL: String;
ImageFileName: String;
SectionName: String);
var
  I: Integer;
begin

  // After much deliberation, CardName ended up being the 'Unique identifyer' or
  // 'Section Name' in the Ini DataBase file. All information regarding a card
  // must now be stored under its URL as a section name/title.
  DataBase.WriteString(SectionName,'Card Name',CardName);
  DataBase.WriteString(SectionName,'Card URL',CardURL);
  DataBase.WriteString(SectionName,'Image File Name',ImageFileName);
  DataBase.WriteString(SectionName,'Image URL',ImageURL);
  DataBase.UpdateFile;
end;

procedure TCardKingdomRow.DownloadImage(URL: String; Path: String);
begin
  ImageRequest := TIdHTTP.Create(nil);
  ImageStream := TMemoryStream.Create;

  ImageRequest.Get(URL, ImageStream);
  ImageStream.Position := 0;
  ImageStream.SaveToFile(Path + '\' + ImageFileName);

  FreeAndNil(ImageStream);
  FreeAndNil(ImageRequest);
end;

//constructor TCardPage.Init(DB: String; DefaultImageSavePath: String);
//begin
//
//  DataBase := TMemIniFile.Create(DB);
//
//  ImagePath := DefaultImageSavePath;
//
//
//  Request := CreateOleObject('MSXML2.XMLHttp.3.0') as IXMLHTTPRequest;
//end;
//
//destructor TCardPage.Destroy;
//begin
//  Inherited;
//  DataBase.Destroy;
//  ImageStream.Destroy;
//  ImageRequest.Destroy;
//  Request := nil;
//end;
//
//procedure TCardPage.GetAll(URL: String);
//begin
//  // Store a local copy, of the Global variable URL.
//    gURL := URL;
//    Request.open('POST',URL,False,EmptyParam,EmptyParam);
//    Request.send('');
//    ParseIt(Request.responseText);
//    DownloadImage;
//    DumpToDB;
//end;
//
//
//procedure TCardPage.ParseIt(HTMLSource: String);
//var
//  AfterThisPoint: Integer;
//  // Image source code link Prefix and Suffix.
//  ImgPreString,ImgPostString,
//  // CardName source code link Prefix and Suffix.
//  CardNamePreString,CardNamePostString: String;
//  // Image URL Start and Finish indexes.
//  ImgS,ImgE,
//  // Card name Start and Finish indexes.
//  CardNameS, CardNameE: Integer;
//begin
//  // All PosEx searches which use this, will begin their search after the beginning
//  // of the main page table of information.
//  AfterThisPoint := Pos('<!-- Column 1 start -->',HTMLSource);
//  /////////////////////////////////////////////////////////
//  // Get the Main Image URL.
//  ImgPreString := '<img src="';
//  ImgPostString := '"';
//  ImgS := PosEx(ImgPreString,HTMLSource,AfterThisPoint) + Length(ImgPreString);
//  ImgE := PosEx(ImgPostString,HTMLSource,ImgS);
//  ImageURL := Copy(HTMLSource,ImgS,ImgE-ImgS);
//  // Gets all characters in the image URL, from the last / to the end of the string.
//  ImageFileName := Copy(ImageURL,LastDelimiter('/',ImageURL)+1, Length(ImageURL)-LastDelimiter('/',ImageURL)+1);
//  /////////////////////////////////////////////////////////
//  // Get the cards' name.
//  CardNamePreString := '<b>';
//  CardNamePostString := '</b>';
//  CardNameS := PosEx(CardNamePreString,HTMLSource,ImgE) + Length(CardNamePreString);
//  CardNameE := PosEx(CardNamePostString,HTMLSource,CardNameS);
//  CardName := Copy(HTMLSource,CardNameS,CardNameE-CardNameS);
//  /////////////////////////////////////////////////////////
//end;

end.
